import axios, {AxiosError, AxiosResponse} from 'axios'
import {Response, TYPE_ENUM} from "@/utils/entity/Response";


export function request<T>(url: string, data: any): Promise<T> {
    return new Promise<T>(function (resolve, reject) {
        axios.post(process.env.VUE_APP_BASE_API_PATH.concat(url), data, {
            headers: {
                "Context-Type": "application/json"
            }
        }).then(function (res: AxiosResponse<Response<T>>) {
            if (res.data.type === TYPE_ENUM.ok) {
                resolve(res.data.result);
            } else {
                reject(res.data);
            }
        }).catch(function (err: AxiosError) {
            alert(err.message);
            reject(err);
        });
    });

}


export default {
    request
}

